/**
 * 
 */
package br.com.buscapecas.validator;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.regex.Pattern;

import br.com.buscapecas.exception.BuscaPecasException;

/**
 * @created 06/05/2010 - 10:10:31
 * @author Maikon Rafael da Rocha
 *
 */
public class IntegerValidator extends Validator {

	private static final Pattern integerPattern = Pattern.compile("([0-9])*");
	private static NumberFormat integerNumberFormat = DecimalFormat.getNumberInstance(ptBR);
	static {
		integerNumberFormat.setMinimumFractionDigits(0);
		integerNumberFormat.setMaximumFractionDigits(0);
	}
	
	/**
	 * 
	 * @param integerValue
	 * @param label
	 * @throws BuscaPecasException
	 * 
	 * @author Maikon Rafael da Rocha
	 * @since 07/07/2010 15:31:32
	 */
	public static void validateInteger(String integerValue, String label) throws BuscaPecasException {
		try {
			if (integerValue != null) {
				if (integerPattern.matcher(integerValue).matches()) {
					integerNumberFormat.parseObject(integerValue);
				} else {
					throw new BuscaPecasException(messagesResources.getString("invalidSingular", new Object[] {label}));
				}
			}
		} catch (Exception e) {
		    throw new BuscaPecasException(messagesResources.getString("invalidSingular", new Object[] {label}));
		}
	}
	
	/**
	 * 
	 * @param messages
	 * @param integerValue
	 * @param label
	 * @throws BuscaPecasException
	 * 
	 * @author Maikon Rafael da Rocha
	 * @since 07/07/2010 15:31:49
	 */
	public static void validateInteger(List<String> messages, String integerValue, String label) {
        try {
            IntegerValidator.validateInteger(integerValue, label);
        } catch (BuscaPecasException e) {
            messages.add(e.getMessage());
        }
    }
	
	/**
	 * 
	 * @param label
	 * @param integerValue
	 * @param labels
	 * 
	 * @author Maikon Rafael da Rocha
	 * @since 05/10/2010 13:36:14
	 */
	public static void validateInteger(String label, String integerValue, List<String> labels) {
        try {
            IntegerValidator.validateInteger(integerValue, label);
        } catch (BuscaPecasException e) {
            labels.add(label);
        }
    }
	
}
